PyInstaller (Create .exe)

Version I used
  • pyinstaller --onefile --noconsole music_player.py

Create an .exe

  • You can create an executable file for your Python application using various tools available. One popular tool for creating standalone executables is PyInstaller . PyInstaller analyzes your Python program to discover every other module and library your program needs in order to execute.

  • Here are the steps to create an executable using PyInstaller:

    • Install PyInstaller : If you haven't installed PyInstaller yet, you can install it via pip:

      • pip install pyinstaller

    • Create the Executable : Navigate to the directory containing your Python script ( music_player.py  in this case) in the command line and run the following command:

      • pyinstaller --onefile music_player.py

      • Replace music_player.py  with the name of your Python script.

    • Find the Executable : Once PyInstaller finishes its job, you'll find a dist  directory created in your project folder. Inside the dist  directory, there will be an executable file named music_player.exe  (or whatever name your Python script had).

  • Now, you can simply double-click on music_player.exe  to run your application without needing to compile the Python code each time.

  • Keep in mind that PyInstaller packages your entire application, including all necessary dependencies, into a single executable file. This means that the executable may be larger in size compared to the original Python script. Additionally, the process might be different if you're using third-party libraries that require special handling.

Add dependencies

  • When you create a standalone executable using PyInstaller, it packages only the files that are explicitly imported or accessed by your Python script. If your script relies on external files like favoritos.json , PyInstaller won't automatically include them in the packaged executable.

  • To resolve this issue, you need to make sure that favoritos.json  is included in the packaged executable. Here's how you can do it:

    • Ensure favoritos.json  is in the same directory as the executable : The simplest solution is to place favoritos.json  in the same directory as the generated executable ( music_player.exe ). This way, your script can access it without any modification.

    • Include favoritos.json  in the PyInstaller bundle : If you want favoritos.json  to be bundled with the executable, you can use the --add-data  option of PyInstaller to include it. For example:

      • pyinstaller --onefile --add-data "favoritos.json;." music_player.py

      • This command tells PyInstaller to include favoritos.json  and place it in the same directory as the executable. You need to adjust the command based on the actual location of favoritos.json  and your Python script.

  • Choose the approach that best fits your needs, and then recompile the executable. Once favoritos.json  is properly included, your executable should be able to access it without any errors.

  • Here, "favoritos.json;."  specifies that you want to include favoritos.json  and place it in the root directory of the bundled executable ( .  denotes the current directory).

    • "favoritos.json"  is the path to the file you want to include.

    • ;  separates the source path from the destination path.

    • .  specifies the destination directory where you want to place the file relative to the root of the bundled executable.

Embed dependencies

  • Yes, you can embed the contents of the JSON file directly into your Python script as a string. This way, you won't need the external JSON file, and your program will remain a single-file executable.

  • Here's how you can do it:

    • Open your favoritos.json  file and copy its contents.

    • Paste the contents as a string in your Python script, assigning it to a variable.

    • Modify your code to use this embedded JSON string instead of reading from the external file.

Remove the Console

  • To prevent the terminal window from appearing when running the executable, you need to use the --noconsole  option with PyInstaller when creating the executable. This option tells PyInstaller not to create a console window along with the GUI window.

  • Here's how you can modify the PyInstaller command to include the --noconsole  option:

    • pyinstaller --onefile --noconsole --add-data "favoritos.json;." music_player.py  (No need to use --add-data if embedded)

  • With this modification, the executable will run without displaying a separate terminal window. Make sure to recompile your executable with this updated command.